<?php
	class LoginController extends CustomControllerAction
	{
		protected $_flash = null;

		public function init() 
		{
			parent::init();
			$this->_flash = $this->_helper->getHelper('FlashMessenger');
		}

		public function indexAction()
		{
			# if the user's already logged in, send them to their account homepage
			$auth = Zend_Auth::getInstance();

			if ($auth->hasIdentity())
				$this->_redirect('/home');
			
			$request = $this->getRequest();

			# determine the page the user was originally trying to request
			$redirect = $request->getPost('redirect');
			if (strlen($redirect) == 0)
				$redirect = $request->getServer('REQUEST_URI');
			if (strlen($redirect) == 0)
				$redirect = '/home';
			
			# process login if request method is post
			if ($request->isPost())
			{
				$user_db_mgr = new UserSDBManager();

				# fetch login details from form and validate them
				$username = $request->getPost('username');
				$password = $request->getPost('password');
				
				if ((strlen($username) == 0) || (strlen($password) == 0))
				{
					if (strlen($username) == 0) 
						$this->_flash->addMessage("Required username field must not be blank!");
					if (strlen($password) == 0)
						$this->_flash->addMessage("Required password field must not be blank!");
				}
				else
				{
					# setup the authentication adapter with db, identity, and credential
					$adapter = new SimpleDBAuthAdapter($username, $password);
					
					# try and authenticate the user
					try
					{
						$result = $auth->authenticate($adapter);
					}
					catch (Amazon_SimpleDB_Exception $e)
					{
						$result = new Zend_Auth_Result(Zend_Auth_Result::FAILURE, null, array("FAIL"));
						$this->_logger->log('(' . get_class($this) . ') ' . $e->getMessage(), Zend_Log::ERR);
						$this->_logger->log('(' . get_class($this) . ') ' . print_r($e, true), Zend_Log::ERR); 
					}
					
					if ($result->isValid())
					{
						# create identity data and write it to session
						$users = $user_db_mgr->getByFieldValue('username', $username);
						$identity = $users[0]->createIdentity();

						# get storage and write identity
						$auth->getStorage()->write($identity);
					}
					else
					{
						$this->_flash->addMessage("Your login details were invalid!");
					}
				}
				
				# redirect user to page desired or (upon failure) back to login
				$this->_redirect($redirect);
			}

			if ($this->_flash->hasMessages()) {
				$this->view->message = $this->_flash->getMessages();
			}

			$prev_addr = split('moblogg.in', $_SERVER['HTTP_REFERER']);
			
			if (sizeof($prev_addr) > 1)
			{
				# set up left tabs	
				$left_tabs = array(
					'Back' => array($prev_addr[1])
				);
			}
			else
			{
				# set up left tabs	
				$left_tabs = array(
					'Moblogg.in' => array('/')
				);
			}

			# set up right tabs
			$right_tabs = array(
				'Login' => array('/login'),
			);

			$this->view->tabs = array('selected' => 'Login', 'left_tabs' => $left_tabs, 'right_tabs' => $right_tabs);
			$this->view->redirect = $redirect;
		}
	}
